package Interface_de_test;

import java.io.IOException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
//import org.eclipse.swt.events.SelectionAdapter;
//import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import swing2swt.layout.BoxLayout;
import org.eclipse.swt.custom.ViewForm;
import swing2swt.layout.BorderLayout;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.CBanner;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.CLabel;
//import org.eclipse.swt.widgets.Text;

import ModStats.CalculStats;
import ModStats.DoCalculs;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;


public class TestInterface{

	/**
     * Launch the application.
     * @param args
	 * @wbp.parser.entryPoint
     */
    public static void GraphicalInterface(final Display display, final Shell shell) {
    	
		    
    	final Display display = Display.getDefault();
        final Shell shell = new Shell();
        
    		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
            shell.setMinimumSize(new Point(130, 20));
            shell.setModified(false);
            shell.setSize(719, 421);
            shell.setMinimized(false);
            shell.setText("STATISTICAL APPLICATION");
            shell.setLayout(new BorderLayout(3, 3));
            Menu menu = new Menu(shell, SWT.BAR);
            shell.setMenuBar(menu);
           
            MenuItem mntmFile_1 = new MenuItem(menu, SWT.CASCADE);
            mntmFile_1.setText("File");
           
            Menu menu_1 = new Menu(mntmFile_1);
            mntmFile_1.setMenu(menu_1);
           
            MenuItem mntmImporterDonnee = new MenuItem(menu_1, SWT.NONE);
            
            mntmImporterDonnee.addSelectionListener(new SelectionAdapter() {
            	@Override
            	public void widgetSelected(SelectionEvent e) {
            		String nomFichier;
            		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
            		dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
            		nomFichier = dialog.open();
            		//Dialog //txtNomFichier;
					//if ((nomFichier != null) && (nomFichier.length() != 0))      
            			//txtNomFichier.setText(nomFichier);
            		//System.out.println("selection du fichier "+nomFichier);
            		DoCalculs.setFileName(nomFichier);
            		//DoCalculs.getFileName();
            		
            	}
            });
            mntmImporterDonnee.setText("Importer Donnee");
           
            MenuItem mntmImporterModule = new MenuItem(menu_1, SWT.NONE);
            mntmImporterModule.setText("Importer Module !");
           
            
            MenuItem mntmQuit = new MenuItem(menu_1, SWT.NONE);
            
            mntmQuit.addSelectionListener(new SelectionAdapter() {
            	@Override
            	public void widgetSelected(SelectionEvent e) {
            		System.exit(0);
            	}
            });
            mntmQuit.setText("Quit");
           
            MenuItem mntmEdit = new MenuItem(menu, SWT.NONE);
            mntmEdit.setText("Edit");
           
            final MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
            mntmHelp.setText("Help");
           
            Menu menu_2 = new Menu(mntmHelp);
            mntmHelp.setMenu(menu_2);
           
            MenuItem mntmHelp_1 = new MenuItem(menu_2, SWT.NONE);
            mntmHelp_1.setText("Help");
           
            MenuItem mntmAbout = new MenuItem(menu_2, SWT.NONE);
            mntmAbout.setText("About");
            
            Composite composite = new Composite(shell, SWT.BORDER);
            composite.setLayoutData(BorderLayout.WEST);
            composite.setLayoutDeferred(true);
            composite.setFont(SWTResourceManager.getFont("Tahoma", 12, SWT.NORMAL));
            composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
            composite.setBounds(0, 0, 69, 20);
            
            final Group grpTest = new Group(shell, SWT.BORDER | SWT.SHADOW_ETCHED_IN);
            grpTest.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.BOLD | SWT.ITALIC));
            grpTest.setText("Informations Panel");
            grpTest.setLayoutData(BorderLayout.SOUTH);
            grpTest.setLayout(null);
            
            CBanner banner = new CBanner(grpTest, SWT.NONE);
            banner.setBounds(46, 42, 20, -1);
//          tabFolder.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));
            
            
            final TabItem[] tbtmtest = new TabItem[30];
            final CLabel[] lblResultLabel = new CLabel[30];
            tbtmtest[0].setControl(lblResultLabel[0]);
            lblResultLabel[0].setText("New Label");
            tbtmtest[0].setText("Results");
            
            //buttons
            final Button btnMoyenne = new Button(grpTest, SWT.NONE);
            btnMoyenne.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Mean Calcul");
            		try {
						lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getMeans());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();						
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            
            final Button btnMdiane = new Button(grpTest, SWT.NONE);
            btnMdiane.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Mediane Calcul");
            		try {
            			lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getMedian());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();						
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            
            final Button btnEcarttype = new Button(grpTest, SWT.NONE);
            btnEcarttype.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Ecart-Type Calcul");
            		try {
            			lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getStandDeviation());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();						
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            
            final Button btnMinMax = new Button(grpTest, SWT.NONE);
            btnMinMax.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Min and Max Calcul");
            		try {
            			lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getMinMax());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            
            
            final Button btnVariance = new Button(grpTest, SWT.NONE);
            btnVariance.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Variance Calcul");
            		try {
            			lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getVariance());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();						
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            
            
            final Button btnMode = new Button(grpTest, SWT.NONE);
            btnMode.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("Mode Calcul");
//            		lblNewLabel.setText("The Mode is : ");
            	}
            });
            
            final Button btnAll = new Button(grpTest, SWT.NONE);
            btnAll.addSelectionListener(new SelectionAdapter() {
            	public void widgetSelected(SelectionEvent e) {
            		tbtmtest[getActiveTabItem(tabFolder)].setText("All Statistical Calcul");
            		try {
            			lblResultLabel[getActiveTabItem(tabFolder)].setText(DoCalculs.getAll());
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();						
						lblResultLabel[getActiveTabItem(tabFolder)].setText("Error : "+e1.toString());
					}
            	}
            });
            composite.setLayout(null);
            
            //btnMinMax.setBounds(361, 10, 68, 23);
            //btnMinMax.setText("Min & Max");
           
            
            Button btnModuleStatistique = new Button(composite, SWT.BORDER);
            btnModuleStatistique.setBounds(0, 10, 100, 23);
            btnModuleStatistique.addSelectionListener(new SelectionAdapter() {
            	@Override
            	public void widgetSelected(SelectionEvent e) {
            		System.out.println("Appui sur le bouton");
            		
            		//
            		//grpTest.setFont(SWTResourceManager.getFont("Tahoma", 9, SWT.BOLD | SWT.ITALIC));
            		grpTest.setText("Statistical Calculs Modul");
            		
            		//buttons 
            		btnMoyenne.setBounds(166, 10, 68, 23);
                    btnMoyenne.setText("Moyenne");
            		
                    btnMdiane.setBounds(243, 10, 68, 23);
                    btnMdiane.setText("Médiane");
                    
                    btnEcarttype.setBounds(320, 10, 68, 23);
                    btnEcarttype.setText("Ecart -Type");
                                      
            		
                    btnMinMax.setBounds(397, 10, 68, 23);
                    btnMinMax.setText("Min & Max");
                    
                    btnVariance.setBounds(474, 10, 68, 23);
                    btnVariance.setText("Variance");
                    
                    btnMode.setBounds(551, 10, 68, 23);
                    btnMode.setText("Mode");
                    
                    btnAll.setBounds(628, 10, 68, 23);
                    btnAll.setText("All");
            	}
            	
            });
            btnModuleStatistique.setSelection(true);
            btnModuleStatistique.setText("Module Statistique");
            
            Composite composite_1 = new Composite(shell, SWT.BORDER);
            composite_1.setLayoutData(BorderLayout.NORTH);
            composite_1.setLayout(new GridLayout(2, false));
            
            Button btnDeleteActiveView = new Button(composite_1, SWT.NONE);
            /*btnDeleteActiveView.addSelectionListener(new SelectionAdapter() {
            	@Override
            	public void widgetSelected(SelectionEvent e) {
            		int activeItem = getActiveTabItem(tabFolder);
            		tbtmtest[getActiveTabItem(tabFolder)].dispose();
            		for(int i = activeItem+1; i < tabFolder.getItemCount(); i++)
            			tbtmtest[i] = tbtmtest[i-1];
            	}
            });
            btnDeleteActiveView.setText("Delete Active view");
            
*/
            

            
//            if(tbtmtest[0] != null)
//            	tbtmtest[0].addListener(SWT.Selection, new Listener() {
//            		public void handleEvent(Event event) {
//            			System.out.println("selected item");
//            		}
//            	}
//            );
            
            //
            
            Button btnNewView = new Button(composite_1, SWT.FLAT | SWT.CENTER);
            
            Composite composite_2 = new Composite(shell, SWT.NONE);
            composite_2.setLayoutData(BorderLayout.CENTER);
            composite_2.setLayout(new FormLayout());
            
            Composite composite_3 = new Composite(composite_2, SWT.NONE);
            composite_3.setLayout(new GridLayout(1, false));
            FormData fd_composite_3 = new FormData();
            fd_composite_3.bottom = new FormAttachment(0, 46);
            fd_composite_3.right = new FormAttachment(0, 604);
            fd_composite_3.top = new FormAttachment(0, 5);
            fd_composite_3.left = new FormAttachment(0, 5);
            composite_3.setLayoutData(fd_composite_3);
            
            TabFolder tabFolder = new TabFolder(composite_2, SWT.NONE);
            FormData fd_tabFolder = new FormData();
            fd_tabFolder.top = new FormAttachment(composite_3, 6);
            fd_tabFolder.bottom = new FormAttachment(100, -10);
            
            Button btnNewButton = new Button(composite_3, SWT.NONE);
            fd_tabFolder.left = new FormAttachment(0, 5);
            fd_tabFolder.right = new FormAttachment(100);
            tabFolder.setLayoutData(fd_tabFolder);
            
            TabItem tbtmTest = new TabItem(tabFolder, SWT.NONE);
            tbtmTest.setText("test1");
            btnNewView.addSelectionListener(new SelectionAdapter() {
            	@Override
            	public void widgetSelected(SelectionEvent e) {
            		int j = tabFolder.getItemCount();
            		tbtmtest[j] = new TabItem(tabFolder, SWT.NONE);
            		tbtmtest[j].setText("Results");
            		
            		//
	        		lblResultLabel[j] = new CLabel(tabFolder, SWT.NONE);
	                tbtmtest[j].setControl(lblResultLabel[j]);
	                lblResultLabel[j].setText("New Label "+j);
            	}
            });
            
            
            
//            CTabItem tbtmResults = new CTabItem(tabFolder, SWT.NONE);
//            tbtmResults.setText("Results");
            
            final TabItem tabItemt;
            
            
            

            
            /*Button btnModuleStatistique_n = new Button(composite_n, SWT.BORDER | SWT.CENTER);
            FormData fd_btnModuleStatistique_n = new FormData();
            fd_btnModuleStatistique_n.left = new FormAttachment(0, 116);
            fd_btnModuleStatistique_n.top = new FormAttachment(0, 5);
            btnModuleStatistique_n.setLayoutData(fd_btnModuleStatistique_n);
            
            btnModuleStatistique_n.setText("Module Statistique");*/
            
    }
    
    public static int getActiveTabItem(TabFolder tabFolder) {
    	int i;
		for(i =0; i< tabFolder.getItemCount(); i++) {
			if(tabFolder.getItem(i).hashCode() == tabFolder.getSelection()[0].hashCode())
				break;
		}
    	return i;
    }
}
